class EventEmitter {
  constructor() {
    this.events = {}
  }

  // 注册事件监听
  on(event, callback) {
    if (!this.events[event]) {
      this.events[event] = []
    }
    this.events[event].push(callback)
  }

  // 移除事件监听
  off(event, callback) {
    if (!this.events[event]) return
    if (!callback) {
      delete this.events[event]
    } else {
      this.events[event] = this.events[event].filter(cb => cb !== callback)
    }
  }

  // 触发事件
  emit(event, data) {
    if (!this.events[event]) return
    this.events[event].forEach(callback => {
      try {
        callback(data)
      } catch (error) {
        console.error('Event callback error:', error)
      }
    })
  }
}

module.exports = EventEmitter 